package com.jzy.stock.wareHourseGuishu.service.impl;

import com.jzy.stock.util.ResultBuildVo;
import com.jzy.stock.util.ResultVo;
import com.jzy.stock.util.StatusEnum;
import com.jzy.stock.wareHourseGuishu.mapper.HouseGuishuMapper;
import com.jzy.stock.wareHourseGuishu.model.*;
import com.jzy.stock.wareHourseGuishu.service.GuishuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 贾志宇
 * @create 2022-07-13-14:13
 */
@Service
public class GuishuServiceImpl implements GuishuService{

    @Autowired
    private HouseGuishuMapper houseGuishuMapper;

    //1.无条件下默认查询全部库房回显

    @Override
    //2.根据勾选的库房查询库区    这里有两个注意点
    //                              1.要避免在for语句里面去操作数据库
    //                              2.SQL中用 foreach 批量查询
    //                              3.注意参数的书写，要满足SQL中的foreach语法。
    public ResultVo queryWareAreaByHouseCode(List<String> stockRoomCodeLists) {
        if (stockRoomCodeLists == null ||stockRoomCodeLists.size() == 0) {
            ResultBuildVo.error("没有选择任何库房");
        }
        //Map<String,List<WareHouseVo>> wareHouseAreaVos = new HashMap<>();
        //要避免在for语句里面去操作数据库，因为数据库的连接数量是有限的，
        //这样可能造成数据库的奔溃，造成请求堵塞之类的问题
        /*List<WareHouseAreaVo> wareHouseAreaAllVoList = new ArrayList<>();
        for (String houseCode : stockRoomCodeLists) {
            List<WareHouseAreaVo> wareHouseAreaVoList = houseGuishuMapper.queryWareAreaByHouseCode(houseCode);
            for(WareHouseAreaVo wareHouseAreaVo : wareHouseAreaVoList){
                wareHouseAreaAllVoList.add(wareHouseAreaVo);
            }
            //wareHouseAreaVos.put(houseCode,wareHouseVoList);
        }*/
        UtilListVo utilListVo = new UtilListVo();
        utilListVo.setStockRoomCodeLists(stockRoomCodeLists);
        List<WareHouseAreaVo> wareHouseAreaAllVoList1 = houseGuishuMapper.queryWareAreaByHouseCode1(utilListVo);
        return ResultBuildVo.success(wareHouseAreaAllVoList1);
    }

    @Override
    public ResultVo queryWareWeiByAreaCode(List<String> wareAreaCodeLists) {
        if (wareAreaCodeLists == null ||wareAreaCodeLists.size() == 0) {
            ResultBuildVo.error("没有选择任何库区");
        }
        //Map<String,List<WareHouseVo>> wareHouseAreaVos = new HashMap<>();
        //要避免在for语句里面去操作数据库，因为数据库的连接数量是有限的，
        //这样可能造成数据库的奔溃，造成请求堵塞之类的问题
        /*List<WareHouseWeiVo> wareHouseWeiAllVoList = new ArrayList<>();
        //要避免在for语句里面去操作数据库，因为数据库的连接数量是有限的，
        //这样可能造成数据库的奔溃，造成请求堵塞之类的问题
        for (String areaCode : wareAreaCodeLists) {
            List<WareHouseWeiVo> wareHouseWeiVoList = houseGuishuMapper.queryWareWeiByAreaCode(areaCode);
            for(WareHouseWeiVo wareHouseWeiVo : wareHouseWeiVoList){
                wareHouseWeiAllVoList.add(wareHouseWeiVo);
            }
            //wareHouseAreaVos.put(houseCode,wareHouseVoList);
        }*/
        UtilListVo utilListVo = new UtilListVo();
        utilListVo.setWareAreaCodeLists(wareAreaCodeLists);
        List<WareHouseWeiVo> wareHouseWeiVoList = houseGuishuMapper.queryWareWeiByAreaCode(utilListVo);
        return ResultBuildVo.success(wareHouseWeiVoList);
    }

    @Override
    //根据法人COde查询库房库区库位三个集合给前端进行回显
    @ApiOperation("根据法人COde查询库房库区库位三个集合给前端进行回显")
    public ResultVo

    queryThreeListByLegalPersonCode(String legalPersonCode) {
        List<FWQListVo> fwqListVos = houseGuishuMapper.queryThreeListByLegalPersonCode(legalPersonCode);

        Set<WareHouseVo> houseVoSet = new HashSet<>();
        //WareHouseVo wareHouseVo = new WareHouseVo();

        Set<WareHouseAreaVo> areaVoSet = new HashSet<>();
        //WareHouseAreaVo wareHouseAreaVo = new WareHouseAreaVo();

        Set<WareHouseWeiVo> wareHouseWeiVoSet = new HashSet<>();
        //WareHouseWeiVo wareHouseWeiVo = new WareHouseWeiVo();

        for(FWQListVo fwqListVo : fwqListVos){
            //这个每个对象不能在全局声明，要不然数据全是第一组数据
            WareHouseWeiVo wareHouseWeiVo = new WareHouseWeiVo();
            WareHouseAreaVo wareHouseAreaVo = new WareHouseAreaVo();
            WareHouseVo wareHouseVo = new WareHouseVo();

            Integer i1 = fwqListVo.getAreaStatus() ? 1 : 0;
            fwqListVo.setAreaStatusName(StatusEnum.queryById(i1));
            Integer i2 = fwqListVo.getRoomStatus() ? 1 : 0;
            fwqListVo.setRoomStatusName(StatusEnum.queryById(i2));
            Integer i3 = fwqListVo.getWeiStatus() ? 1 : 0;
            fwqListVo.setWeiStatusName(StatusEnum.queryById(i3));

            wareHouseWeiVo.setCode(fwqListVo.getWeiCode());
            wareHouseWeiVo.setName(fwqListVo.getWeiName());
            wareHouseWeiVo.setRemark(fwqListVo.getWeiRemark());
            wareHouseWeiVo.setStatus(fwqListVo.getWeiStatus());
            wareHouseWeiVo.setStatusName(fwqListVo.getWeiStatusName());
            wareHouseWeiVoSet.add(wareHouseWeiVo);

            wareHouseVo.setCode(fwqListVo.getRoomCode());
            wareHouseVo.setName(fwqListVo.getRoomName());
            wareHouseVo.setRemark(fwqListVo.getRoomRemark());
            wareHouseVo.setStatus(fwqListVo.getRoomStatus());
            wareHouseVo.setStatusName(fwqListVo.getRoomStatusName());
            houseVoSet.add(wareHouseVo);

            wareHouseAreaVo.setCode(fwqListVo.getAreaCode());
            wareHouseAreaVo.setName(fwqListVo.getAreaName());
            wareHouseAreaVo.setRemark(fwqListVo.getAreaRemark());
            wareHouseAreaVo.setStatus(fwqListVo.getAreaStatus());
            wareHouseAreaVo.setStatusName(fwqListVo.getAreaStatusName());
            areaVoSet.add(wareHouseAreaVo);
        }


        ThreeSetVo threeListVo = new ThreeSetVo();
        threeListVo.setWareHouseVoList(houseVoSet);
        threeListVo.setWareHouseAreaVoList(areaVoSet);
        threeListVo.setWareHouseWeiVoList(wareHouseWeiVoSet);

        return ResultBuildVo.success(threeListVo);
    }

    @Override
    //归属修改
    public ResultVo updateWareHouseGuishu(GuishuUpdateVo guishuUpdateVo) {
        //String legalPersonCode = guishuUpdateVo.getLegalPersonCode();

        houseGuishuMapper.updateWareHouseGuishu(guishuUpdateVo);
        //一定要避免在for中操作数据库
        /*for(String weiCode : weiCodeLists){
            GuishuUpdateVo guishuUpdateVo1 = new GuishuUpdateVo();
            guishuUpdateVo1.setLegalPersonCode(legalPersonCode);
            guishuUpdateVo1.setWeiCode(weiCode);
            houseGuishuMapper.updateWareHouseGuishu(guishuUpdateVo1);
        }*/
        return ResultBuildVo.success();
    }

    /*
     @Override
     //弃用
     public ResultVo queryWareWeiByLegalPersonCode(String legalPersonCode) {
     List<WareHouseWeiVo> wareHouseWeiVoList =
     houseGuishuMapper.queryWareWeiByLegalPersonCode(legalPersonCode);
     for(WareHouseWeiVo wareHouseWeiVo : wareHouseWeiVoList){
     Integer i = wareHouseWeiVo.getStatus() ? 1 : 0;
     wareHouseWeiVo.setStatusName(StatusEnum.queryById(i));
     }
     return ResultBuildVo.success(wareHouseWeiVoList);
     }

     @Override
     //弃用
     public ResultVo queryWareAreaByWeiCode(List<String> weiCodeLists) {
     if (weiCodeLists == null ||weiCodeLists.size() == 0) {
     ResultBuildVo.error("没有选择任何库位");
     }
     //Map<String,List<WareHouseVo>> wareHouseAreaVos = new HashMap<>();
     List<WareHouseAreaVo> wareHouseAreaAllVoList = new ArrayList<>();
     for (String weiCode : weiCodeLists) {
     List<WareHouseAreaVo> wareHouseAreaVoList = houseGuishuMapper.queryWareAreaByWeiCode(weiCode);
     for(WareHouseAreaVo wareHouseAreaVo : wareHouseAreaVoList){
     wareHouseAreaAllVoList.add(wareHouseAreaVo);
     }
     //wareHouseAreaVos.put(houseCode,wareHouseVoList);
     }
     return ResultBuildVo.success(wareHouseAreaAllVoList);
     }

     @Override
     //弃用
     public ResultVo queryWareHouseByAreaCode(List<String> areaCodeLists) {
     if (areaCodeLists == null ||areaCodeLists.size() == 0) {
     ResultBuildVo.error("没有选择任何库区");
     }
     //Map<String,List<WareHouseVo>> wareHouseAreaVos = new HashMap<>();
     List<WareHouseVo> wareHouseAllVoList = new ArrayList<>();
     for (String areaCode : areaCodeLists) {
     List<WareHouseVo> wareHouseVoList = houseGuishuMapper.queryWareHouseByAreaCode(areaCode);
     for(WareHouseVo wareHouseVo : wareHouseVoList){
     wareHouseAllVoList.add(wareHouseVo);
     }
     //wareHouseAreaVos.put(houseCode,wareHouseVoList);
     }
     return ResultBuildVo.success(wareHouseAllVoList);
     }*/
}
